import pygame
import sys
from game import Game
from bullet import Bullet
from bullet2 import Bullet2
from pygame.locals import *
from player1 import Player1
from player2 import Player2
from wall import Wall


def game_star():
    pygame.init()
    # , FULLSCREEN | HWSURFACE, 32

    Game.window = pygame.display.set_mode((Game.W, Game.H))

    pygame.display.set_caption("游戏零零")

    Game.bg = pygame.image.load("img/背景(1700x900)22.png")
    Background_img_rect = Game.bg.get_rect()

    Game.music = pygame.mixer.Sound("wav/游戏.mp3")
    Game.music.play(-1)

    Game.zixinag_img = pygame.image.load("img/纸箱复制.png")

    FPS = 60
    clock = pygame.time.Clock()

    Game.player1_x = 250
    Game.player1_y = 250

    Game.player1 = Player1(Game.player1_x, Game.player1_y)
    Game.playerGroup = pygame.sprite.Group()
    Game.playerGroup.add(Game.player1)

    Game.player2_x = 1350
    Game.player2_y = 650

    Game.player2 = Player2(Game.player2_x, Game.player2_y)
    Game.playerGroup2 = pygame.sprite.Group()
    Game.playerGroup2.add(Game.player2)

    Game.bullet = Bullet(0, 0, "left", 10)
    Game.bullet2 = Bullet2(0, 0, "left", 10)
    Game.wall = Wall()
    Game.level_text = "关卡1"
    # [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    # [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    # [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3],
    Game.wall_list = [
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3],
    ]

    Game.fashe_music = pygame.mixer.Sound("Wav/发射.wav")
    Game.jump_music = pygame.mixer.Sound("wav/跳.wav")

    while True:
        Game.xiangzishuliang += 1
        Game.xiangzishuliang2 += 1
        Game.index += 1
        clock.tick(FPS)
        game_get()

        Game.window.blit(Game.bg, Background_img_rect)
        Game.wall.wall_display()
        player_ico()
        text()
        if Game.shengli:
            Game.playerGroup.update(Game.index)
            Game.playerGroup.draw(Game.window)
            Game.player1.confine()
            Game.player1.left_right()
            Game.player1.hit_wall()

            Game.playerGroup2.update(Game.index)
            Game.playerGroup2.draw(Game.window)
            Game.player2.confine()
            Game.player2.left_right()
            Game.player2.hit_wall()

        # 子弹绘制
        for bullet in Game.bullet_list:
            bullet.bullet_fire()
            bullet.bullet_display()
            if bullet.Bullet_rect.left <= 0 or bullet.Bullet_rect.left >= Game.W:
                Game.bullet_list.remove(bullet)

        for bullet2 in Game.bullet_list2:
            bullet2.bullet_fire()
            bullet2.bullet_display()
            if bullet2.Bullet_rect.left <= 0 or bullet2.Bullet_rect.left >= Game.W:
                Game.bullet_list2.remove(bullet2)

        for bullet in Game.bullet_list:
            if Game.player2.rect.colliderect(bullet.Bullet_rect):
                if Game.player2.rect.x > Game.player1.rect.x:
                    Game.player2.rect.x += 10
                    Game.player2.rect.y -= 4
                    Game.direction_A2 = False
                    Game.direction_D2 = False
                    Game.player2.box_w -= 1

                if Game.player2.rect.x < Game.player1.rect.x:
                    Game.player2.rect.x -= 10
                    Game.player2.rect.y -= 4
                    Game.direction_A2 = False
                    Game.direction_D2 = False
                    Game.player2.box_w -= 1

        for bullet2 in Game.bullet_list2:
            if Game.player1.rect.colliderect(bullet2.Bullet_rect):
                if Game.player2.rect.x > Game.player1.rect.x:
                    Game.player1.rect.x -= 10
                    Game.player1.rect.y -= 4
                    Game.direction_A = False
                    Game.direction_D = False
                    Game.player1.box_w -= 1

                if Game.player2.rect.x < Game.player1.rect.x:
                    Game.player1.rect.x += 10
                    Game.player1.rect.y -= 4
                    Game.direction_A = False
                    Game.direction_D = False
                    Game.player1.box_w -= 1

        # 重力
        Game.G_speed += Game.G  # 增加垂直速度
        Game.player1.rect.y += Game.G_speed  # 根据垂直速度更新位置

        Game.G_speed2 += Game.G2  # 增加垂直速度
        Game.player2.rect.y += Game.G_speed2  # 根据垂直速度更新位置

        recur()
        wall_hit_player2()
        wall_hit_player1()
        pygame.display.update()


def player_ico():
    Game.tubiao_1 = pygame.image.load("img/图标黄色.png")
    Game.tubiao_huang = pygame.transform.scale(Game.tubiao_1, (70, 70))
    Game.tubiao_2 = pygame.image.load("img/图标绿色.png")
    Game.tubiao_lv = pygame.transform.scale(Game.tubiao_2, (70, 70))
    for i in range(0, Game.player1_ming):
        Game.window.blit(Game.tubiao_huang, (20 + i * 80, 90))
    for i in range(0, Game.player2_ming):
        Game.window.blit(Game.tubiao_lv, (1500 - i * 80, 90))


def text():
    guanqia_font = pygame.font.Font("Font/font.ttf", 50)
    shuliang_font = pygame.font.Font("Font/font.ttf", 50)

    score_text = shuliang_font.render("Player1 HP:" + str(Game.player1.box_w), True, (255, 200, 0))
    Game.window.blit(score_text, (20, 10))

    level_text = guanqia_font.render("Player2 HP:" + str(Game.player2.box_w), True, (0, 255, 0))
    Game.window.blit(level_text, (1250, 20))


def game_get():
    for event in pygame.event.get():

        mouse_x, mouse_y = pygame.mouse.get_pos()
        # print(mouse_x, mouse_y)
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_r:
                Game.music.stop()
                Game().__init__()
                game_star()

            # Game.player1
            if event.key == pygame.K_w and Game.jump <= 1:
                Game.jump += 1
                Game.G_speed = -Game.speed
                Game.jump_music.play()
                print("w")
                Game.direction_W = True
                if Game.jump >= 3:
                    Game.jump = 0

            if event.key == pygame.K_a:
                print("a")
                Game.direction_A = True
                Game.current_images = Game.player1.images_left

            if event.key == pygame.K_d:
                print("d")
                Game.direction_D = True
                Game.current_images = Game.player1.images_right

            if event.key == pygame.K_s:
                Game.fangkuai = True

            if event.key == pygame.K_j:
                if len(Game.bullet_list) < 5:
                    Game.fashe_music.play()
                    if Game.direction == "left":
                        bullet_right = Bullet(Game.player1.rect.left, Game.player1.rect.top + 20, "left", 15)
                        Game.bullet_list.append(bullet_right)

                    elif Game.direction == "right":
                        bullet_left = Bullet(Game.player1.rect.left, Game.player1.rect.top + 20, "right", 15)
                        Game.bullet_list.append(bullet_left)

            if event.key == pygame.K_UP and Game.jump2 <= 1:
                Game.jump2 += 1
                Game.G_speed2 = -Game.speed2
                Game.direction_W2 = True
                Game.jump_music.play()
                Game.direction_2 = True
                if Game.jump2 >= 3:
                    Game.jump2 = 0

            if event.key == pygame.K_KP0:
                if len(Game.bullet_list2) < 5:
                    Game.fashe_music.play()

                    if Game.direction2 == "left":
                        bullet_right2 = Bullet2(Game.player2.rect.left, Game.player2.rect.top + 20, "left", 15)
                        Game.bullet_list2.append(bullet_right2)

                    elif Game.direction2 == "right":
                        bullet_left2 = Bullet2(Game.player2.rect.left, Game.player2.rect.top + 20, "right", 15)
                        Game.bullet_list2.append(bullet_left2)

            if event.key == pygame.K_LEFT:
                Game.direction_A2 = True

            if event.key == pygame.K_RIGHT:
                Game.direction_D2 = True

            if event.key == pygame.K_DOWN:
                Game.fangkuai2 = True

            if event.key == pygame.K_SPACE:
                Game.kongge = True

        if event.type == pygame.KEYUP:
            # Game.player1
            if event.key == pygame.K_a:
                Game.direction_A = False
                print("松开a")

            if event.key == pygame.K_d:
                Game.direction_D = False
                print("松开d")
            if event.key == pygame.K_s:
                Game.fangkuai = False

            if event.key == pygame.K_LEFT:
                Game.direction_A2 = False

            if event.key == pygame.K_RIGHT:
                Game.direction_D2 = False

            if event.key == pygame.K_DOWN:
                Game.fangkuai2 = False

            if event.key == pygame.K_SPACE:
                Game.kongge = False

            if event.key == pygame.K_p:
                chuangkou = open("data/窗口大小.txt", "w", encoding="utf-8")

                if Game.quanping:
                    Game.window = pygame.display.set_mode((Game.W, Game.H), FULLSCREEN | HWSURFACE, 32)
                    chuangkou.write("全屏模式")
                    chuangkou.close()
                    Game.quanping = False
                else:
                    Game.window = pygame.display.set_mode((Game.W, Game.H))
                    Game.quanping = True
                    chuangkou.write("窗口模式")
                    chuangkou.close()


def recur():
    if Game.player1.rect.y >= Game.H or Game.player2.rect.y >= Game.H:
        Game.sisheng = pygame.mixer.Sound("wav/死声.wav")
        Game.sisheng.play()
        Game.music.stop()
        Game().__init__()
        game_star()

    if Game.player1.box_w <= 0:
        Game.sisheng = pygame.mixer.Sound("wav/死声.wav")
        Game.sisheng.play()
        Game.player1.box_w = 100
        Game.player1.rect.x = Game.player1_x
        Game.player1.rect.y = Game.player1_y
        Game.player1_ming -= 1

    if Game.player2.box_w <= 0:
        Game.sisheng = pygame.mixer.Sound("wav/死声.wav")
        Game.sisheng.play()
        Game.player2.box_w = 100
        Game.player2.rect.x = Game.player2_x
        Game.player2.rect.y = Game.player2_y
        Game.player2_ming -= 1

    elif Game.player1_ming <= 0:
        Game.shengli = False
        font = pygame.font.Font("Font/font.ttf", 220)
        score_text = font.render(" 恭喜您成功 ", True, (8, 25, 15), (255, 85, 0))
        score_text2 = font.render(" Player2获胜！ ", True, (8, 25, 15), (255, 85, 0))
        Game.window.blit(score_text, (50, 200))
        Game.window.blit(score_text2, (50, 450))

    elif Game.player2_ming <= 0:
        Game.shengli = False
        font = pygame.font.Font("Font/font.ttf", 220)
        score_text = font.render(" 恭喜您成功 ", True, (8, 25, 15), (255, 85, 0))
        score_text2 = font.render(" Player1获胜！ ", True, (8, 25, 15), (255, 85, 0))
        Game.window.blit(score_text, (50, 200))
        Game.window.blit(score_text2, (50, 450))
    else:
        Game.shengli = True


def wall_hit_player1():
    for row in range(Game.MAP_HEIGHT):
        for col in range(Game.MAP_WIDTH):
            wall_rect = pygame.Rect(col * Game.BLOCK_SIZE, row * Game.BLOCK_SIZE, Game.BLOCK_SIZE,
                                    Game.BLOCK_SIZE)
            if Game.wall_list[row][col] in (4, 2, 1):
                if Game.player1.rect.colliderect(wall_rect):
                    if Game.player1.rect.bottom >= wall_rect.top:
                        Game.player1.rect.y = wall_rect.y - Game.player1.rect.height
                        Game.G_speed = 0  # 碰撞后重置垂直速度
                        Game.direction_W = False
                        Game.jump = 0

            if Game.wall_list[row][col] in (1, 2):
                for bullet in Game.bullet_list:
                    if bullet.Bullet_rect.colliderect(wall_rect):
                        Game.bullet_list.remove(bullet)
                        pengzhuang_music = pygame.mixer.Sound("wav/碰撞.wav")
                        pengzhuang_music.play()

            if Game.wall_list[row][col] == 3:
                wall_rect = pygame.Rect(col * Game.BLOCK_SIZE, row * Game.BLOCK_SIZE + 30, Game.BLOCK_SIZE,
                                        Game.BLOCK_SIZE - 30)
                if Game.player1.rect.colliderect(wall_rect):
                    Game.sisheng = pygame.mixer.Sound("wav/死声.wav")
                    Game.sisheng.play()
                    Game.player1.rect.x = Game.player1_x
                    Game.player1.rect.y = Game.player1_y
                    Game.player1.box_w = 100
                    Game.player1_ming -= 1

            if Game.wall_list[row][col] == 0:
                wall_rect = pygame.Rect(col * Game.BLOCK_SIZE, row * Game.BLOCK_SIZE + 30, Game.BLOCK_SIZE,
                                        Game.BLOCK_SIZE - 30)
                if Game.player1.rect.colliderect(
                        wall_rect) and Game.fangkuai and Game.direction == "right" and Game.xiangzishuliang >= 30:
                    try:
                        if Game.wall_list[row + 1][col + 2] == 0:
                            Game.wall_list[row + 1][col + 2] = 4
                            Game.xiangzishuliang = 0
                        Game.fangkuai = False
                    except:
                        Game.fangkuai = False
                if Game.player1.rect.colliderect(
                        wall_rect) and Game.fangkuai and Game.direction == "left" and Game.xiangzishuliang >= 30:
                    try:
                        if Game.wall_list[row + 1][col - 1] == 0:
                            Game.wall_list[row + 1][col - 1] = 4
                            Game.xiangzishuliang = 0
                        Game.fangkuai = False
                    except:
                        Game.fangkuai = False

            if Game.wall_list[row][col] == 4:
                for bullet in Game.bullet_list:
                    if bullet.Bullet_rect.colliderect(wall_rect):
                        Game.bullet_list.remove(bullet)
                        Game.wall_list[row][col] = 0
                        pengzhuang_music = pygame.mixer.Sound("wav/碰撞.wav")
                        pengzhuang_music.play()

            if Game.wall_list[row][col] == 5:
                if Game.player1.rect.colliderect(wall_rect):
                    Game.wall_list[row][col] = 0
                    chibaoshi_music = pygame.mixer.Sound("wav/吃宝石.wav")
                    chibaoshi_music.play()
                    Game.player1.box_w += 50


def wall_hit_player2():
    for row in range(Game.MAP_HEIGHT):
        for col in range(Game.MAP_WIDTH):
            wall_rect = pygame.Rect(col * Game.BLOCK_SIZE, row * Game.BLOCK_SIZE, Game.BLOCK_SIZE,
                                    Game.BLOCK_SIZE)

            if Game.wall_list[row][col] in (1, 2, 4):
                if Game.player2.rect.top <= wall_rect.bottom:
                    if Game.player2.rect.colliderect(wall_rect):
                        Game.player2.rect.y = wall_rect.y - Game.player2.rect.height
                        Game.G_speed2 = 0  # 碰撞后重置垂直速度
                        Game.direction_W2 = False
                        Game.jump2 = 0

            if Game.wall_list[row][col] in (1, 2):
                for bullet2 in Game.bullet_list2:
                    if bullet2.Bullet_rect.colliderect(wall_rect):
                        Game.bullet_list2.remove(bullet2)
                        pengzhuang_music = pygame.mixer.Sound("wav/碰撞.wav")
                        pengzhuang_music.play()

            if Game.wall_list[row][col] == 3:
                wall_rect = pygame.Rect(col * Game.BLOCK_SIZE, row * Game.BLOCK_SIZE + 30, Game.BLOCK_SIZE,
                                        Game.BLOCK_SIZE - 30)
                if Game.player2.rect.colliderect(wall_rect):
                    Game.sisheng = pygame.mixer.Sound("wav/死声.wav")
                    Game.sisheng.play()
                    Game.player2.rect.x = Game.player2_x
                    Game.player2.rect.y = Game.player2_y
                    Game.player2.box_w = 100
                    Game.player2_ming -= 1

            if Game.wall_list[row][col] == 0:
                wall_rect = pygame.Rect(col * Game.BLOCK_SIZE, row * Game.BLOCK_SIZE + 30, Game.BLOCK_SIZE,
                                        Game.BLOCK_SIZE - 30)

                if Game.player2.rect.colliderect(
                        wall_rect) and Game.fangkuai2 and Game.direction2 == "right" and Game.xiangzishuliang2 >= 30:
                    try:
                        if Game.wall_list[row + 1][col + 2] == 0:
                            Game.wall_list[row + 1][col + 2] = 4
                            Game.xiangzishuliang2 = 0

                        Game.fangkuai2 = False
                    except:
                        Game.fangkuai2 = False
                if Game.player2.rect.colliderect(
                        wall_rect) and Game.fangkuai2 and Game.direction2 == "left" and Game.xiangzishuliang2 >= 30:
                    try:
                        if Game.wall_list[row + 1][col - 1] == 0:
                            Game.wall_list[row + 1][col - 1] = 4
                            Game.xiangzishuliang2 = 0
                        Game.fangkuai2 = False
                    except:
                        Game.fangkuai2 = False

            if Game.wall_list[row][col] == 6:
                if Game.player2.rect.colliderect(wall_rect):
                    Game.wall_list[row][col] = 0
                    chibaoshi_music = pygame.mixer.Sound("wav/吃宝石.wav")
                    chibaoshi_music.play()
                    Game.player2.box_w += 50

            if Game.wall_list[row][col] == 4:
                for bullet2 in Game.bullet_list2:
                    if bullet2.Bullet_rect.colliderect(wall_rect):
                        Game.bullet_list2.remove(bullet2)
                        Game.wall_list[row][col] = 0
                        pengzhuang_music = pygame.mixer.Sound("wav/碰撞.wav")
                        pengzhuang_music.play()


if __name__ == '__main__':
    Game().__init__()
    game_star()
